package com.wocao.controller.uniapp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wocao.entity.*;
import com.wocao.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/uni-app")
public class AppLogin {

    @Autowired
    private ZyOwnerRoomService zyOwnerRoomService;

    @Autowired
    private ZyOwnerService zyOwnerService;

    @Autowired
    private ZyVisitorService zyVisitorService;

    //    小区
    @Autowired
    private ZyCommunityService zyCommunityService;

    //     楼栋
    @Autowired
    private ZyBuildingService zyBuildingService;

    //    单元号
    @Autowired
    private ZyUnitService zyUnitService;

    //    房间号
    @Autowired
    private ZyRoomService zyRoomService;


    @Autowired
    private ZyRepairService zyRepairService;


    /**
     * uni app登录
     *
     * @param login 登录
     * @return return
     */
    @PostMapping("/appLogin")
    public ResponseResult postLogin(@RequestBody ZyOwner login) {
        try {
            ZyOwner owner = zyOwnerService.getLogin(login);
            return ResponseResult.ok(owner);
        } catch (Exception e) {
            return ResponseResult.error(500L, e.getMessage());
        }
    }

    /**
     * 注册
     *
     * @param zyOwner z
     * @return z
     */
    @PutMapping("/addRegister")
    public ResponseResult addRegister(@RequestBody ZyOwner zyOwner) {
        try {
            zyOwnerService.addRegister(zyOwner);
            return ResponseResult.ok("新增成功");
        } catch (Exception e) {
            return ResponseResult.error(500L, e.getMessage());
        }
    }

    /**
     * 获取用户头像
     */
    @GetMapping("/ownerAvatar")
    public ResponseResult getAvatar(String ownerPhoneNumber) {
        ZyOwner owner = zyOwnerService.getOne(new QueryWrapper<ZyOwner>().eq("owner_phone_number", ownerPhoneNumber));
        return ResponseResult.ok(owner.getOwnerPortrait());
    }

    /**
     * 判断是否为业主
     */
    @GetMapping("judgeowner/{ownerId}")
    public ResponseResult JudgeOwner(@PathVariable String ownerId) {
        List<ZyOwnerRoom> list = zyOwnerRoomService.list(new QueryWrapper<ZyOwnerRoom>().eq("owner_id", ownerId));
        if (list != null && list.size() > 0) {
            return ResponseResult.ok(true);
        } else {
            return ResponseResult.ok(false);
        }
    }

    @PostMapping("/addZyVisitor")
    public ResponseResult addZyVisitor(@RequestBody ZyVisitor zyVisitor) {
        try {
            LocalDateTime now = LocalDateTime.now();
            zyVisitor.setCreateTime(now);
            zyVisitorService.save(zyVisitor);
            return ResponseResult.ok("成功");
        } catch (Exception e) {
            return ResponseResult.error(500L, "失败");
        }
    }

    @GetMapping("communities")
    public ResponseResult<ZyCommunity> findCommunity() {
        try {
            List<ZyCommunity> communities = zyCommunityService.list();
            return ResponseResult.ok(communities);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500l, e.getMessage());
        }
    }


    @GetMapping("ZyRepairAll")
    public ResponseResult<ZyRepair> ZyRepairAll() {
        try {
            List<ZyRepair> zyRepairAll = zyRepairService.findZyRepairAll();
            return ResponseResult.ok(zyRepairAll);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500L, e.getMessage());
        }
    }


    @GetMapping("ZyRepairPending")
    public ResponseResult<ZyRepair> ZyRepairPending() {
        try {
            List<ZyRepair> zyRepairAll = zyRepairService.findZyRepairPending();
            return ResponseResult.ok(zyRepairAll);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500L, e.getMessage());
        }
    }


    @GetMapping("ZyRepairDecision")
    public ResponseResult<ZyRepair> ZyRepairDecision() {
        try {
            List<ZyRepair> zyRepairAll = zyRepairService.findZyRepairDecision();
            return ResponseResult.ok(zyRepairAll);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500L, e.getMessage());
        }
    }

    /**
     * 级联
     *
     * @return
     */
    @GetMapping("ZyCommunity")
    public ResponseResult<ZyCommunity> ZyCommunity() {
        try {
            List<ZyCommunity> list = zyCommunityService.list();
            return ResponseResult.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500L, e.getMessage());
        }
    }

    @GetMapping("ZyBuilding/{communityId}")
    public ResponseResult<ZyBuilding> ZyBuilding(@PathVariable String communityId) {
        try {
            List<ZyBuilding> list = zyBuildingService.list(new QueryWrapper<ZyBuilding>().eq("community_id", communityId));
            return ResponseResult.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500L, e.getMessage());
        }
    }

    @GetMapping("ZyUnit/{buildingId}")
    public ResponseResult<ZyUnit> ZyUnit(@PathVariable String buildingId) {
        try {
            List<ZyUnit> list = zyUnitService.list(new QueryWrapper<ZyUnit>().eq("building_id", buildingId));
            return ResponseResult.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500L, e.getMessage());
        }
    }


    @GetMapping("ZyRoom/{unitId}")
    public ResponseResult<ZyRoom> ZyRoom(@PathVariable String unitId) {
        try {
            List<ZyRoom> list = zyRoomService.list(new QueryWrapper<ZyRoom>().eq("unit_id", unitId));
            return ResponseResult.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error(500L, e.getMessage());
        }
    }


    // 生成带有时间戳的报修编号
    private String generateRepairNum() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String timestamp = sdf.format(new Date());
        return "BX_" + timestamp;
    }

    @PostMapping("/addZyRepair")
    public ResponseResult<ZyRepair> addZyRepair(@RequestBody ZyRepair zyRepair) {
        LocalDateTime now = LocalDateTime.now();
        zyRepair.setReceivingOrdersTime(now);
        zyRepair.setRepairNum(generateRepairNum());
        System.err.println(zyRepair);
        try {
            zyRepairService.save(zyRepair);
            return ResponseResult.ok("新增成功");
        } catch (Exception e) {
            return ResponseResult.error(500L, "失败");
        }
    }
}
